package hhm.spring.springmvc;

import hhm.spring.pojo.HHMResponse;
import hhm.spring.springmvc.base.BaseController;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/13 0013.
 */
@Controller
@RequestMapping("/ra/quartz")
@ResponseBody
public class QuartzController extends BaseController {

    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    private static Map<String, String> cornMap1 = new HashMap<String, String>();
    private static Map<String, String> cornMap2 = new HashMap<String, String>();

    static {

        cornMap1.put("每3秒", "0/3 * * * * ?");
        cornMap1.put("每15秒", "0/15 * * * * ?");
        cornMap1.put("每分钟", "0 * * * * ?");
        cornMap1.put("每小时", "0 0 * * * ?");
        cornMap1.put("每天", "0 0 0 * * ?");
        cornMap1.put("每月", "0 0 0 0 * ?");


        cornMap2.put("0/3 * * * * ?", "每3秒");
        cornMap2.put("0/10 * * * * ?", "每10秒");
        cornMap2.put("0/15 * * * * ?", "每15秒");
        cornMap2.put("0 * * * * ?", "每分钟");
        cornMap2.put("0 0 * * * ?", "每小时");
        cornMap2.put("0 0 0 * * ?", "每天");
        cornMap2.put("0 0 0 0 * ?", "每月");

    }

    /**
     * 更新时间定时器中的执行时间
     *
     * @return
     */
    @RequestMapping(value = "/updateJobCron", method = RequestMethod.POST)
    public HHMResponse updateJobCron(String triggerName, String value) {

        try {

            Scheduler scheduler = schedulerFactoryBean.getScheduler();
            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName);
            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cornMap1.get(value));
            trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
            scheduler.rescheduleJob(triggerKey, trigger);
            hhmResponse.setSuccess(true).setMsg("设置成功");
        } catch (SchedulerException e) {
            e.printStackTrace();
        }


        return hhmResponse;

    }


    /**
     * 获取时间定时器中的执行时间，要根据表达式来解析
     *
     * @return
     */
    @RequestMapping("/getSchedulerByName")
    public HHMResponse getSchedulerByName(String triggerName) {


        try {

            Scheduler scheduler = schedulerFactoryBean.getScheduler();
            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName);
            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            hhmResponse.setSuccess(true).setMsg(cornMap2.get(trigger.getCronExpression()));

        } catch (SchedulerException e) {
            e.printStackTrace();
        }


        return hhmResponse;

    }


}
